﻿using ConsoleApp1.Utility;
using Microsoft.EntityFrameworkCore;
using Sample.ConsoleApp.CodeFist.Sqlite.Entitys;

namespace Sample.ConsoleApp.CodeFist.Sqlite.DbContexts;

/// <summary>
/// 数据库上下文
/// </summary>
/// <param name="options"></param>
internal class MyDb1Context : DbContext
{
    public DbSet<MyEntity> MyEntitys { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.ApplyConfiguration(new MyEntityTypeConfiguration());

        /*
        modelBuilder.Entity<MyEntity>()
            .Property(e => e.Data)
            .HasConversion(new JsonDocumentToStringConverter());
        */
    }

    /// <summary>
    /// 注意：无参构造函数，必须重写 OnConfiguring() 函数
    /// </summary>
    /// <param name="optionsBuilder"></param>
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        base.OnConfiguring(optionsBuilder);

        var directoryHelper = new DirectoryHelper();
        string connectionString = directoryHelper.GetSqliteConnectionString();
        optionsBuilder.UseSqlite(connectionString);
    }
}
